package com.lizheng.yunservice.goods.service.impl;

import com.lizheng.yunservice.common.domain.ApiRestResponse;
import com.lizheng.yunservice.goods.mapper.VtpGoodsMapper;
import com.lizheng.yunservice.goods.model.VtpGoods;
import com.lizheng.yunservice.goods.model.VtpGoodsVo;
import com.lizheng.yunservice.goods.service.IVtpGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@Service
public class VtpGoodsServiceImpl implements IVtpGoodsService {

    @Autowired
    private VtpGoodsMapper vtpGoodsMapper;

    @Override
    public ApiRestResponse<Void> updateForwardNum(String goodsId) {
        // 调用 Mapper 更新转发数
        int affectedRows = vtpGoodsMapper.updateForwardNum(goodsId);

        // 如果更新成功（affectedRows > 0），返回成功响应
        if (affectedRows > 0) {
            return ApiRestResponse.ok();
        } else {
            // 如果更新失败（如 goodsId 不存在），返回错误响应
            return ApiRestResponse.fail("更新转发数失败，商品ID可能不存在");
        }
    }

    @Override
    public VtpGoodsVo getGoodsDetail(Long id) {
        VtpGoods goods = vtpGoodsMapper.selectById(id);
        if (goods == null) return null;
        VtpGoodsVo vo = new VtpGoodsVo();
        vo.setGoodsId(goods.getGoods_id());
        vo.setTitle(goods.getTitle());
        vo.setIntroduction(goods.getIntroduction());
        vo.setLogoUrl(goods.getLogo_url());
        vo.setBackgroundImgUrl(goods.getBackground_img_url());
        vo.setOriginImgUrl(goods.getOrigin_img_url());
        vo.setVideoUrl(goods.getVideo_url());
        vo.setProdPlace(goods.getProd_place());
        vo.setLongitude(goods.getLongitude() == null ? null : new java.math.BigDecimal(goods.getLongitude()));
        vo.setLatitude(goods.getLatitude() == null ? null : new java.math.BigDecimal(goods.getLatitude()));
        vo.setGoodsSku(goods.getGoods_sku());
        vo.setDescription(goods.getDescription());
        vo.setPartYear(parseDate(goods.getPart_year()));
        vo.setCategoryId(goods.getCategory_id() == null ? null : goods.getCategory_id().intValue());
        vo.setSoldNum(goods.getSold_num() == null ? null : goods.getSold_num().intValue());
        vo.setStockNum(goods.getStock_num() == null ? null : goods.getStock_num().intValue());
        vo.setShowStatus(goods.getShow_status() == null ? null : Integer.valueOf(goods.getShow_status()));
        vo.setOringinalPrice(goods.getOringinal_price() == null ? null : goods.getOringinal_price().intValue());
        vo.setSalesPrice(goods.getSales_price() == null ? null : goods.getSales_price().intValue());
        vo.setLikesNum(goods.getLikes_num() == null ? null : goods.getLikes_num().intValue());
        vo.setCollectNum(goods.getCollect_num() == null ? null : goods.getCollect_num().intValue());
        vo.setViewNum(goods.getView_num() == null ? null : goods.getView_num().intValue());
        vo.setForwardNum(goods.getForward_num() == null ? null : goods.getForward_num().intValue());
        vo.setPreparedNum(goods.getPrepared_num() == null ? null : goods.getPrepared_num().intValue());
        vo.setRecommend(goods.getRecommend() == null ? null : goods.getRecommend().intValue());
        vo.setCommentNum(goods.getComment_num() == null ? null : goods.getComment_num().intValue());
        vo.setCommentScore(goods.getComment_score() == null ? null : new java.math.BigDecimal(goods.getComment_score()));
        vo.setGoodMarkNum(goods.getGood_mark_num() == null ? null : goods.getGood_mark_num().intValue());
        vo.setCommonMarkNum(goods.getCommon_mark_num() == null ? null : goods.getCommon_mark_num().intValue());
        vo.setBadMarkNum(goods.getBad_mark_num() == null ? null : goods.getBad_mark_num().intValue());
        vo.setCreateTime(goods.getCreate_time());
        vo.setUpdateTime(goods.getUpdate_time());
        // TODO: categoryName, isLike, isCollect, imgs, recommendTime等业务字段
        return vo;
    }

    @Override
    public List<VtpGoodsVo> getGoodsRecomList(Integer limit) {
        List<VtpGoods> goodsList = vtpGoodsMapper.selectRecomGoodsList(limit);
        List<VtpGoodsVo> voList = new ArrayList<>();
        for (VtpGoods goods : goodsList) {
            VtpGoodsVo vo = new VtpGoodsVo();
            vo.setGoodsId(goods.getGoods_id());
            vo.setTitle(goods.getTitle());
            vo.setIntroduction(goods.getIntroduction());
            vo.setLogoUrl(goods.getLogo_url());
            vo.setBackgroundImgUrl(goods.getBackground_img_url());
            vo.setOriginImgUrl(goods.getOrigin_img_url());
            vo.setVideoUrl(goods.getVideo_url());
            vo.setProdPlace(goods.getProd_place());
            vo.setLongitude(goods.getLongitude() == null ? null : new java.math.BigDecimal(goods.getLongitude()));
            vo.setLatitude(goods.getLatitude() == null ? null : new java.math.BigDecimal(goods.getLatitude()));
            vo.setGoodsSku(goods.getGoods_sku());
            vo.setDescription(goods.getDescription());
            vo.setPartYear(parseDate(goods.getPart_year()));
            vo.setCategoryId(goods.getCategory_id() == null ? null : goods.getCategory_id().intValue());
            vo.setSoldNum(goods.getSold_num() == null ? null : goods.getSold_num().intValue());
            vo.setStockNum(goods.getStock_num() == null ? null : goods.getStock_num().intValue());
            vo.setShowStatus(goods.getShow_status() == null ? null : Integer.valueOf(goods.getShow_status()));
            vo.setOringinalPrice(goods.getOringinal_price() == null ? null : goods.getOringinal_price().intValue());
            vo.setSalesPrice(goods.getSales_price() == null ? null : goods.getSales_price().intValue());
            vo.setLikesNum(goods.getLikes_num() == null ? null : goods.getLikes_num().intValue());
            vo.setCollectNum(goods.getCollect_num() == null ? null : goods.getCollect_num().intValue());
            vo.setViewNum(goods.getView_num() == null ? null : goods.getView_num().intValue());
            vo.setForwardNum(goods.getForward_num() == null ? null : goods.getForward_num().intValue());
            vo.setPreparedNum(goods.getPrepared_num() == null ? null : goods.getPrepared_num().intValue());
            vo.setRecommend(goods.getRecommend() == null ? null : goods.getRecommend().intValue());
            vo.setCommentNum(goods.getComment_num() == null ? null : goods.getComment_num().intValue());
            vo.setCommentScore(goods.getComment_score() == null ? null : new java.math.BigDecimal(goods.getComment_score()));
            vo.setGoodMarkNum(goods.getGood_mark_num() == null ? null : goods.getGood_mark_num().intValue());
            vo.setCommonMarkNum(goods.getCommon_mark_num() == null ? null : goods.getCommon_mark_num().intValue());
            vo.setBadMarkNum(goods.getBad_mark_num() == null ? null : goods.getBad_mark_num().intValue());
            vo.setCreateTime(goods.getCreate_time());
            vo.setUpdateTime(goods.getUpdate_time());
            // TODO: categoryName, isLike, isCollect, imgs, recommendTime等业务字段
            voList.add(vo);
        }
        return voList;
    }

    private Date parseDate(String dateStr) {
        if (dateStr == null) return null;
        try {
            return new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }
}
